package com.winsdom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.dto.CategoryTreeNodes;
import com.winsdom.dto.OrganizationMegAddDTO;
import com.winsdom.dto.OrganizationMegDTO;
import com.winsdom.result.Result;
import com.winsdom.service.IOrganizationService;
import com.winsdom.vo.OrganizationMegVO;
import com.winsdom.vo.UserAndOrgPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.winsdom.enums.AuthenticationType.ORGANIZATION_MANAGEMENT;

@RestController
@RequestMapping("/organization")
@Api(tags = "组织管理")
@Slf4j
@Authentication(value = ORGANIZATION_MANAGEMENT)
public class OrganizationController {
    @Autowired
    private IOrganizationService iOrganizationService;

    @GetMapping("/tree")
    @Operation(summary = "获取登录账号下的组织树", description = "获取登录账号下的组织树（先登录，自动获取账号id）")
    @OperationLog(description = "获取登录账号下的组织树")
    public Result getOrganizationTree() {
        List<CategoryTreeNodes> categoryTreeNodes = iOrganizationService.getOrganizationTree();
        log.info("组织树:{}", categoryTreeNodes);
        return Result.success(categoryTreeNodes);
    }


    @GetMapping("/orgmsg")
    @ApiOperation(value = "根据组织id获取组织信息")
    @OperationLog(description = "根据组织id获取组织信息")
    public Result getOrgMsg(@Parameter(description = "组织ID") @RequestParam Long categoryId) {
        log.info("组织id:{}",categoryId);
        OrganizationMegVO organizationMegVO=iOrganizationService.getOrganizationMsg(categoryId);

        log.info("组织信息:{}",organizationMegVO);
        return Result.success(organizationMegVO);
    }

    @GetMapping("/usermsg")
    @ApiOperation(value = "根据组织id获取员工信息")
    @OperationLog(description = "根据组织id获取员工信息")
    public Result getOrgUsers(
            @Parameter(description = "组织ID") @RequestParam Long categoryId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNum,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Long pageSize) {
        log.info("组织id:{}",categoryId);
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        Page<UserAndOrgPageVo> userMsg = iOrganizationService.getUserMsg(categoryId, pageNum, pageSize);

        log.info("员工信息:{}",userMsg.getRecords());
        return Result.success(userMsg);
    }

    @PostMapping("/create")
    @Operation(summary = "创建组织", description = "创建组织信息")
    @OperationLog(description = "创建组织信息")
    public Result createOrg(@RequestBody OrganizationMegAddDTO organizationMegDTO) {
        log.info("新增组织信息：{}",organizationMegDTO);
        iOrganizationService.insertOrg(organizationMegDTO,organizationMegDTO.getUserCategoryId());
        return Result.success("组织创建成功");
    }

    @PostMapping("/update")
    @Operation(summary = "修改组织", description = "修改组织信息")
    @OperationLog(description = "修改组织信息")
    public Result updateOrg(@RequestBody OrganizationMegDTO organizationMegDTO) {
        log.info("接收修改的组织信息：{}",organizationMegDTO);
        iOrganizationService.updateOrg(organizationMegDTO);
        return Result.success("组织修改成功");
    }

    @GetMapping("/delete/{categoryId}/{userCategoryId}")
    @Operation(summary = "删除组织", description = "删除组织信息")
    @OperationLog(description = "删除组织信息")
    public Result deleteOrg(@Parameter(description = "组织分类ID", required = true) @PathVariable Long categoryId,
                            @Parameter(description = "用户组织ID", required = true) @PathVariable Long userCategoryId) {
        log.info("删除组织id:{}",categoryId);
        log.info("用户的组织id:{}",userCategoryId);
        iOrganizationService.deleteOrg(categoryId,userCategoryId);
        return Result.success("组织删除成功");
    }
}
